﻿@model TopicContainer
@using PopForums.Controllers;
@using PopForums.Extensions;
@using PopForums.Models;
@{
	ViewBag.Title = Model.Topic.Title;
    Layout = "~/Areas/PopForums/Views/Shared/PopForumsMaster.cshtml";
}

@section HeaderContent {
<script src="~/scripts/tiny_mce/jquery.tinymce.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function () {
		PopForums.topicSetup(@Model.Topic.TopicID, @Model.PagerContext.PageIndex, @Model.PagerContext.PageCount);
	});
</script>
}

<div id="TopBreadcrumb" class="navAbsolute">
	<h1>@Model.Topic.Title</h1>
	<p>
		@Html.ActionLink(PopForums.Resources.Recent, "Recent", ForumController.Name, new { page = 1 }, null)
		^ @Html.ActionLink(PopForums.Resources.Forums, "Index", ForumHomeController.Name, null, null)
		-&gt; @Html.ActionLink(Model.Forum.Title, "Index", "Forum", new { urlName = Model.Forum.UrlName, page = 1 }, null)
	</p>
</div>

@if (this.GetUserFromViewData() != null) {
	<p id="AsyncResponse"></p>
	<div class="topicUserTools">
		<input type="button" value="@(Model.IsSubscribed ? PopForums.Resources.Unsubscribe : PopForums.Resources.Subscribe)" id="SubscribeButton" class="toolButton" />
		<input type="button" value="@(Model.IsFavorite ? PopForums.Resources.FavoriteRemove : PopForums.Resources.FavoriteMake)" id="FavoriteButton" class="toolButton" />
	</div>
}

@if (this.IsUserInRole(PermanentRoles.Moderator))
{
	<input id="TopicModLogButton" class="toolButton" type="button" value="@PopForums.Resources.ModerationLog" />
	<div class="moderationLog" id="TopicModerationLog"></div>
}

@Html.PagerLinks("Forum", "Topic", Model.PagerContext, "pagerLinks", "morePager", "currentPager")

<div id="PostStream">
@if (Model.PagerContext.PageIndex > 1)
{
	<input type="button" class="previousPostsButton" value="@PopForums.Resources.ShowPreviousPosts" />
}

@foreach(var post in Model.Posts) {
	Html.RenderPartial("PostItem", post, new ViewDataDictionary {{ "VotedPostIDs", Model.VotedPostIDs }, { "Signatures", Model.Signatures }, { "Avatars", Model.Avatars }, {ViewDataDictionaries.ViewDataUserKey, this.GetUserFromViewData()} });
}

@if (Model.PagerContext.PageIndex < Model.PagerContext.PageCount)
{
	<input type="button" class="morePostsButton" value="@PopForums.Resources.ShowMorePosts" />
}
</div>

@Html.Hidden("LastPostID", Model.Posts.Last().PostID, new { @class = "lastPostID" })
	
@Html.PagerLinks("Forum", "Topic", Model.PagerContext, "pagerLinks", "morePager", "currentPager")

<div id="StreamBottom"></div>

@if (Model.PermissionContext.UserCanPost)
{
	<div id="NewReply" style="display: none;"></div>
	<input type="button" value="@PopForums.Resources.PostReply" id="ReplyButton" class="toolButton" />
}
else
{
	<p>@Model.PermissionContext.DenialReason</p>
}

@if (this.IsUserInRole(PermanentRoles.Moderator))
{
	<div id="ModeratorPanel">
		<h3>@PopForums.Resources.Moderator</h3>
		<p>
			@Html.CheckBox("CloseOnReply") <label for="CloseOnReply">@PopForums.Resources.CloseOnReply</label><br />
			@using (Html.BeginForm("ToggleDeleted", "Moderator", new { id = Model.Topic.TopicID }, FormMethod.Post, new { @class = "inlineModForm" }))
			{
				<input type="submit" value="@(Model.Topic.IsDeleted ? PopForums.Resources.Undelete : PopForums.Resources.Delete)" />
			}
			@using (Html.BeginForm("TogglePin", "Moderator", new { id = Model.Topic.TopicID }, FormMethod.Post, new { @class = "inlineModForm" }))
			{
				<input type="submit" value="@(Model.Topic.IsPinned ? PopForums.Resources.Unpin : PopForums.Resources.Pin)" />
			}
			@using (Html.BeginForm("ToggleClosed", "Moderator", new { id = Model.Topic.TopicID }, FormMethod.Post, new { @class = "inlineModForm" }))
			{
				<input type="submit" value="@(Model.Topic.IsClosed ? PopForums.Resources.Open : PopForums.Resources.Close)" />
			}
		</p>
		@using (Html.BeginForm("UpdateTopic", "Moderator"))
		{
			<p>
				@Html.Hidden("TopicID", Model.Topic.TopicID)
				@PopForums.Resources.Title: @Html.TextBox("NewTitle", Model.Topic.Title)<br />
				@PopForums.Resources.Forum: 				
				<select id="NewForum" name="NewForum">
					@foreach (var forum in ((CategorizedForumContainer)ViewBag.CategorizedForums).UncategorizedForums)
						{<option value="@forum.ForumID"@Html.Raw(forum.ForumID == Model.Topic.ForumID ? @" selected=""selected""" : "")>@forum.Title</option>}
					@foreach (var category in ((CategorizedForumContainer)ViewBag.CategorizedForums).CategoryDictionary)
					{
						<optgroup label="@category.Key.Title">
							@foreach (var forum in category.Value)
								{<option value="@forum.ForumID"@Html.Raw(forum.ForumID == Model.Topic.ForumID ? @" selected=""selected""" : "")>@forum.Title</option>}
						</optgroup>
					}
				</select>
				<input type="submit" value="@PopForums.Resources.Update" />
			</p>
		}
	</div>
}